﻿
using Microsoft.EntityFrameworkCore;
using PetAdoptApi.Database;
using PetAdoptApi.Entity;
using PetAdoptApi.Services.Auth;
using PetAdoptionShared.Dto;

namespace PetAdoptApi.Endpoints;

public class AuthEndpoint 
{
    public static void Map(RouteGroupBuilder rgb)
    {
        rgb.MapPost("login", Login);
    }

    private static async Task<Res> Login(LoginReq dto, AppDbContext db, TokenService tokenService)
    {
        User? user = await db.Users.FirstOrDefaultAsync(u => u.Name == dto.userName);
        if (user == null) return Res.Fail("username/password not correct!");
        //todo check credential
        var token = tokenService.GenJwt(user);
        return Res.Ok(token);
    }
}
